import { getData, setData, data } from './dataStore';
import fs from 'fs';
import { QuizList, Error, QuizId, Success } from './interface';
import { isValidToken } from './check';

export function DeleteQuestion
(token: string, quizId: number, TargetQuestionId: number) {
    const data = getData();
    
    const authUserId = isValidToken(token);
    if (!authUserId) {
        return { error: 'Token is empty or invalid (does not refer to valid logged in user session)' };
    }
    const quizIndex = data.quizzes.findIndex(quiz => quiz.QuizID === quizId);
    if (quizIndex === -1) {
        return { error: 'Quiz ID does not refer to a valid quiz.' };
    }

    const quiz = data.quizzes.find(quiz => quiz.QuizID === quizId);
    if (!quiz.AuthorID.includes(authUserId)) {
        return { error: 'Quiz ID does not refer to a quiz that this user owns.' };
    }

    if(!quiz.Questions.some((question: { questionId: number; }) => question.questionId === TargetQuestionId)) {
        return { error: "Question Id does not refer to a valid question within this quiz." };
    }

    let questionIndex = quiz.Questions.findIndex((question: { questionId: number; }) => question.questionId === TargetQuestionId);

    if (questionIndex === -1) {
        return { error: "Question Id does not refer to a valid question within this quiz" };
    } else {
        data.quizzes[questionIndex].Questions.splice(questionIndex, 1);
    }

    return {};

}


export function MoveQuestion (quizId: number, questionId: number, token: string, newposition: number) {
    const data = getData();
    
    const authUserId = isValidToken(token);
    if (!authUserId) {
        return { error: 'Token is empty or invalid (does not refer to valid logged in user session)' };
    }
    const quizIndex = data.quizzes.findIndex(quiz => quiz.QuizID === quizId);
    if (quizIndex === -1) {
        return { error: 'Quiz ID does not refer to a valid quiz.' };
    }

    const Targetquiz = data.quizzes[quizIndex];
    if (!Targetquiz.AuthorID.includes(authUserId)) {
        return { error: 'Quiz ID does not refer to a quiz that this user owns.' };
    }

    if(!Targetquiz.Questions.some((question: { questionId: number; }) => question.questionId === questionId)) {
        return { error: "Question Id does not refer to a valid question within this quiz." };
    }

    if(newposition < 0 || newposition > (Targetquiz.Questions.length - 1)) {
        return {error: 
        "NewPosition is less than 0, or NewPosition is greater than n-1 where n is the number of questions"
        };
    }

    const CurrentIndex = Targetquiz.Questions.findIndex((q: { questionId: number; }) => q.questionId === questionId);
    
    if (newposition === CurrentIndex) {
        return {error: "NewPosition is the position of the current question"};
    }
    
    let temp = Targetquiz.Questions[CurrentIndex];
    Targetquiz.Questions[CurrentIndex] = Targetquiz.Questions[newposition];
    Targetquiz.Questions[newposition] = temp;
    
    data.quizzes[quizIndex].TimeLastEdited = new Date();

    return {};

}







